﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Configuration;
using System.Data;
using Newtonsoft.Json;
using System.Web.Script.Services;
using System.Globalization;

namespace PromotionEngine
{
   

    public partial class CustomerGrade : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.CustomerGrade> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.CustomerGrade> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }

        public string sysFormat = "";
        protected void Page_Load(object sender, EventArgs e)
        {
             sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetODataCustomerGradeList()
        {
            List<Model.CustomerGrade> lstCustomerGrade = new List<Model.CustomerGrade>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCustomerGrade(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.CustomerGrade customerGrade = new Model.CustomerGrade();
                    customerGrade.CompanyCode = r["CompanyCode"].ToString();
                    customerGrade.CompanyName = r["CompanyName"].ToString();
                    customerGrade.GradeCode = r["GradeCode"].ToString();
                    customerGrade.GradeName = r["GradeName"].ToString();
                    customerGrade.GradeID = r["GradeID"].ToString();
                    customerGrade.MinValue = double.Parse( r["MinValue"].ToString());
                    if (r["PointCurrency"].ToString().Length > 0)
                    {
                        customerGrade.PointCurrency = double.Parse(r["PointCurrency"].ToString());
                    }
                    else
                    {
                          customerGrade.PointCurrency = 0;
                    }
                    if (r["PointFactor"].ToString().Length > 0)
                    {
                        customerGrade.PointFactor = double.Parse(r["PointFactor"].ToString());
                    }
                    else
                    {
                        customerGrade.PointFactor = 0;
                    }
                    customerGrade.CreatedUserID = r["CreatedUserID"].ToString();
                    if (r["CreatedDate"].ToString().Length > 0)
                    {
                        customerGrade.CreatedDate = DateTime.Parse(r["CreatedDate"].ToString());
                    }
                    lstCustomerGrade.Add(customerGrade);
                }
                return new Response(lstCustomerGrade, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateCustomerGrade(string oCustomerGrade, bool isInsert)
        {
            Model.CustomerGrade customerGrade = JsonConvert.DeserializeObject<Model.CustomerGrade>(oCustomerGrade);
            if (isInsert)
            {
                customerGrade.CreatedDate = DateTime.Now;

                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        customerGrade.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        customerGrade.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();

            string errMsg = pmt.UpdateJsonCustomerGrade(customerGrade.GradeID, customerGrade.GradeCode, customerGrade.GradeName,
                    customerGrade.CompanyCode, customerGrade.CreatedUserID, customerGrade.MinValue,customerGrade.PointFactor,customerGrade.PointCurrency,
                     ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {

            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteCustomerGrade(string gradeID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonCustomerGrade(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), gradeID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetCustomerGradeList()
        {
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllCustomerGrade(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
                bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
    }
}